package com.fsquare.shipping.api.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.broadleafcommerce.core.order.domain.FulfillmentOption;
import org.broadleafcommerce.core.order.fulfillment.domain.BandedPriceFulfillmentOption;
import org.broadleafcommerce.core.order.fulfillment.domain.FulfillmentPriceBand;
import org.broadleafcommerce.core.web.api.endpoint.order.FulfillmentEndpoint;
import org.broadleafcommerce.core.web.api.wrapper.FulfillmentGroupItemWrapper;
import org.broadleafcommerce.core.web.api.wrapper.FulfillmentGroupWrapper;
import org.broadleafcommerce.core.web.api.wrapper.FulfillmentOptionWrapper;
import org.broadleafcommerce.core.web.api.wrapper.OrderWrapper;

import com.fsquare.shipping.api.wrapper.BandedPriceFulfillmentOptionWrapper;
import com.fsquare.shipping.api.wrapper.ExtFulfillmentOptionWrapper;
import com.fsquare.shipping.api.wrapper.FulfillmentPriceBandWrapper;
import com.fsquare.shipping.service.ExtFulfillmentOptionService;


public class FulfillmentWS extends FulfillmentEndpoint{
	
	
	@Resource(name = "extFulfillmentOptionService")
    protected ExtFulfillmentOptionService extFulfillmentOptionService;
	
    public List<FulfillmentGroupWrapper> findFulfillmentGroupsForOrder(HttpServletRequest request) {
        return super.findFulfillmentGroupsForOrder(request);
    }

    public OrderWrapper removeAllFulfillmentGroupsFromOrder(HttpServletRequest request, boolean priceOrder) {
        return super.removeAllFulfillmentGroupsFromOrder(request, priceOrder);
    }


    public FulfillmentGroupWrapper addFulfillmentGroupToOrder(HttpServletRequest request,
            FulfillmentGroupWrapper wrapper, boolean priceOrder) {
        return super.addFulfillmentGroupToOrder(request, wrapper, priceOrder);
    }


    public FulfillmentGroupWrapper addItemToFulfillmentGroup(HttpServletRequest request,
            Long fulfillmentGroupId,
            FulfillmentGroupItemWrapper wrapper,
            boolean priceOrder) {
        return super.addItemToFulfillmentGroup(request, fulfillmentGroupId, wrapper, priceOrder);
    }

	public FulfillmentGroupWrapper addFulfillmentOptionToFulfillmentGroup(HttpServletRequest request, Long fulfillmentGroupId, Long fulfillmentOptionId, boolean priceOrder) {
		return super.addFulfillmentOptionToFulfillmentGroup(request, fulfillmentGroupId, fulfillmentOptionId, priceOrder);
	}

	public List<FulfillmentOptionWrapper> findFulfillmentOptions(HttpServletRequest request, String fulfillmentType) {
		return super.findFulfillmentOptions(request, fulfillmentType);
	}

	public FulfillmentOptionWrapper findFulfillmentOptionByid(HttpServletRequest request, Long fulfillmentOptionId) {
		FulfillmentOption fulfillmentOption = fulfillmentOptionService.readFulfillmentOptionById(fulfillmentOptionId);
		FulfillmentOptionWrapper fulfillmentOptionWrapper = (FulfillmentOptionWrapper) context.getBean(FulfillmentOptionWrapper.class.getName());
        fulfillmentOptionWrapper.wrapDetails(fulfillmentOption, request);
		return fulfillmentOptionWrapper;
	}
	
	public List<FulfillmentPriceBandWrapper> readFulfillmentOptionPricebandsByFulfillmentOption(HttpServletRequest request, Long fulfillmentOptionId) {
		List<FulfillmentPriceBand> list = extFulfillmentOptionService.readFulfillmentOptionPricebandsByFulfillmentOption(fulfillmentOptionId);
		List<FulfillmentPriceBandWrapper> wrapperList = new ArrayList<FulfillmentPriceBandWrapper>();
		for(FulfillmentPriceBand fulfillmentPriceBand: list){
			FulfillmentPriceBandWrapper wrapper = (FulfillmentPriceBandWrapper) context.getBean(FulfillmentPriceBandWrapper.class.getName());
			wrapper.wrapDetails(fulfillmentPriceBand, request);
			wrapperList.add(wrapper);
		}

		return wrapperList;
	}
	
	public FulfillmentPriceBandWrapper readFulfillmentOptionPricebandsById(HttpServletRequest request, Long id) {
		FulfillmentPriceBand fulfillmentPriceBand = extFulfillmentOptionService.readFulfillmentOptionPricebandById(id);
		FulfillmentPriceBandWrapper wrapper = (FulfillmentPriceBandWrapper) context.getBean(FulfillmentPriceBandWrapper.class.getName());
		wrapper.wrapDetails(fulfillmentPriceBand, request);
		return wrapper;
	}
	
	public BandedPriceFulfillmentOptionWrapper readBandedPriceFulfillmentOptionWrapperById(HttpServletRequest request, Long id) {
		FulfillmentOption fulfillmentOption = extFulfillmentOptionService.readFulfillmentOptionById(id);
		BandedPriceFulfillmentOptionWrapper wrapper = (BandedPriceFulfillmentOptionWrapper) context.getBean(BandedPriceFulfillmentOptionWrapper.class.getName());
		wrapper.wrapDetails(fulfillmentOption, request);
		return wrapper;
	}

	public List<BandedPriceFulfillmentOptionWrapper> readAllBandedPriceFulfillmentOptionWrapper(HttpServletRequest request) {
		List<FulfillmentOption> list = extFulfillmentOptionService.readAllFulfillmentOptions();
		
		List<BandedPriceFulfillmentOptionWrapper> wrapperList = new ArrayList<BandedPriceFulfillmentOptionWrapper>();
		for(FulfillmentOption fulfillmentOption: list){
			BandedPriceFulfillmentOptionWrapper wrapper = (BandedPriceFulfillmentOptionWrapper) context.getBean(BandedPriceFulfillmentOptionWrapper.class.getName());
			wrapper.wrapDetails((BandedPriceFulfillmentOption)fulfillmentOption, request);
			wrapperList.add(wrapper);
		}
		
		return wrapperList;
	}
	
	public BandedPriceFulfillmentOption addBandedPriceFulfillmentOption(HttpServletRequest request, BandedPriceFulfillmentOptionWrapper wrapper) {
		BandedPriceFulfillmentOption fulfillmentOption = wrapper.unwrap(request, context);
		extFulfillmentOptionService.save(fulfillmentOption);
		return fulfillmentOption;
	}
	
	public FulfillmentPriceBand addFulfillmentPriceBand(HttpServletRequest request, FulfillmentPriceBandWrapper wrapper) {
		FulfillmentPriceBand fulfillmentPriceBand = wrapper.unwrap(request, context);
		fulfillmentPriceBand = extFulfillmentOptionService.saveFulfillmentPriceBand(fulfillmentPriceBand);
		return fulfillmentPriceBand;
	}
	
	public FulfillmentOption addFulfillmentOption(HttpServletRequest request, FulfillmentOptionWrapper fulfillmentOptionWrapper) {
		ExtFulfillmentOptionWrapper wrapper = (ExtFulfillmentOptionWrapper)fulfillmentOptionWrapper;
		FulfillmentOption fulfillmentOption = wrapper.unwrap(request, context);
		extFulfillmentOptionService.save(fulfillmentOption);
//
//	            if (fulfillmentGroupRequest.getOrder() != null && fulfillmentGroupRequest.getOrder().getId().equals(cart.getId())) {
//	                try {
//	                    fulfillmentGroupRequest.setOrder(cart);
//	                    FulfillmentGroup fulfillmentGroup = fulfillmentGroupService.addFulfillmentGroupToOrder(fulfillmentGroupRequest, priceOrder);
//	                    FulfillmentGroupWrapper fulfillmentGroupWrapper = (FulfillmentGroupWrapper) context.getBean(FulfillmentGroupWrapper.class.getName());
//	                    fulfillmentGroupWrapper.wrapDetails(fulfillmentGroup, request);
//	                    return fulfillmentGroupWrapper;
//	                } catch (PricingException e) {
//	                    throw new WebApplicationException(e, Response.status(Response.Status.INTERNAL_SERVER_ERROR)
//	                            .type(MediaType.TEXT_PLAIN).entity("An error occured pricing the cart.").build());
//	                }
//	            }
////	        }
//	        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
//	                .type(MediaType.TEXT_PLAIN).entity("Cart could not be found").build());
		
		return fulfillmentOption;
    }

	public void removeBandedPriceFulfillmentOption(HttpServletRequest request,
			Long id) {
		// TODO Auto-generated method stub
		
	}

	public void removeFulfillmentPriceBand(HttpServletRequest request, Long id) {
		// TODO Auto-generated method stub
		
	}

	public void removeFulfillmentOption(HttpServletRequest request, Long id) {
		// TODO Auto-generated method stub
		
	}

}
